package com.blog.wu.myblog.config;

import com.blog.wu.myblog.base.BaseService;
import com.blog.wu.myblog.entity.UserInfo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author wuyanshen
 * @Date 2018-10-12 下午2:08
 * @Discription 防止非法登录过滤器
 */
@WebFilter("/*")
public class LoginFilter extends BaseService implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("--- LoginFilter初始化 ---");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //获取HttpServletRequest
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String uri = request.getRequestURI();
        logger.info("请求的url是：{}", uri);


        //从HttpSession中获取用户信息
//        UserInfo user = (UserInfo) request.getSession().getAttribute("userinfo");
//        if (uri.contains("/login")) {
//            filterChain.doFilter(servletRequest, servletResponse);
//        } else if (user != null) {
//            filterChain.doFilter(servletRequest, servletResponse);
//        } else {
//            request.getRequestDispatcher("/back/login").forward(servletRequest, servletResponse);
//        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        logger.info("--- LoginFilter销毁 ---");
    }
}
